﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TamTreSoSinh.DBContext;

namespace TamTreSoSinh.BO
{
    public class Gallery
    {
        public List<GalleryInfo> GetAllGallery()
        {
            List<GalleryInfo> galleryInfo = new List<GalleryInfo>();

            using(DBTamTreEntities db = new DBTamTreEntities()){
                var lstImg = db.GalleryImages.Where(obj => obj.IsOnHomePage).OrderBy(obj => obj.Priority).ToList();
                if (lstImg == null)
                {
                    return null;
                }

                foreach (var img in lstImg)
                {
                    GalleryInfo galleryImg = GalleryInfo.FromGalleyImage(img);
                    galleryInfo.Add(galleryImg);
                }
            }

            return galleryInfo;
        }

        public string GetImgPathById(int id)
        {
            using (DBTamTreEntities db = new DBTamTreEntities())
            {
                var path = db.GalleryImages.Where(obj => obj.Id == id).SingleOrDefault();
                if (path == null)
                {
                    return null;
                }

                return path.ImagePath;
            }
        }

        public GalleryInfo FirstGallery()
        {
            using (DBTamTreEntities db = new DBTamTreEntities())
            {
                var imgFirst = db.GalleryImages.Where(obj => obj.IsOnHomePage).OrderBy(obj => obj.Priority).FirstOrDefault();

                return GalleryInfo.FromGalleyImage(imgFirst);
            }
        }
    }

    public class GalleryInfo
    {
        public int Id { get; set; }
        public string LinkVideo { get; set; }
        public string Name { get; set; }

        public static GalleryInfo FromGalleyImage(GalleryImage gallery)
        {
            GalleryInfo galleryInfo = new GalleryInfo{
                Id = gallery.Id,
                LinkVideo = gallery.ImagePath,
                Name = gallery.ImageName
            };

            return galleryInfo;
        }
    }
}